import org.junit.Test;

public class PETScSolverTest {

	static {
		try {
			// System.load("/usr/lib/openmpi/lib/libmca_common_sm.so");
			// System.load("/usr/lib/openmpi/lib/libmpi.so");
			System.load("/opt/petsc/petsc-dev/lib/libpetsc.so");
			System.load("/home/hbui/workspace2/jni/PETScSolver/Release/libPETScSolver.so");
		} catch (UnsatisfiedLinkError e) {
			System.err
					.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n"
							+ e);
			System.exit(1);
		}
	}

	@Test
	public void test1() {
		int n = 3;
		double val[] = { 1, 1, 0, 1, 1, 1, 0, 1, 1 };
		double b[] = { 1, 2, 3 };
		double[] x = new double[n];

		String[] argv = new String[] { "-ksp_view" };
		PETScSolver s = new PETScSolver(argv);
		// PETScSolver s = new PETScSolver();
		s.setSize(n);
		s.initialize();

		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				s.assembleLHS(i, j, val[n * i + j]);
			}
			s.assembleRHS(i, b[i]);
		}

		s.setPC("jacobi");

		s.solve(x);

		for (int i = 0; i < n; i++) {
			System.out.println(x[i]);
		}

		// s.finalize();
		s.delete();
	}
}
